import 'package:flutter/material.dart';
import 'package:rxdart/rxdart.dart';

class RxDartDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('RxDartDemo'),
        elevation: 0.0,
      ),
      body: RxDartDemoHome(),
    );
  }
}

class RxDartDemoHome extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => RxDartDemoState();
}

class RxDartDemoState extends State<RxDartDemoHome> {
//  Observable<String> _observable;

  Observable<String> observable;

  @override
  void initState() {
    super.initState();

//    observable = Observable.periodic(Duration(seconds: 3), (x) => x.toString());

//    observable = Observable.just('james');
//
//    observable = Observable.fromFuture(Future.value('hello'));
//
//    _observable = Observable.fromIterable(['我的', '小可爱']);
//
//    _observable = Observable(Stream.fromIterable(['ninghao,lielie']));

//    observable.listen(print);

    PublishSubject<String> publishSubject =
        PublishSubject(); //Subject 相当于StreamController

    publishSubject
        .listen((data) => {print('listen 1 =$data')}); //publishsubject 想监听再添加数据
    publishSubject.add('james');

    publishSubject.listen((data) => {print('listen 2 =$data')});

    publishSubject.add('hola');

    BehaviorSubject<String> behaviorSubject = BehaviorSubject(); //监听最后一个数据

    behaviorSubject.add('我的');
    behaviorSubject.add('my');
    behaviorSubject
        .listen((data) => {print('listen  behaviorSubject1 =$data')});
    behaviorSubject
        .listen((data) => {print('listen  behaviorSubject2 =$data')});
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
